package com.gxb.codebook;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatEditText;

public class LineEditText extends AppCompatEditText {
    private Rect mRect;
    private Paint mPaint;

    public LineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setColor(Color.TRANSPARENT); // 设置为透明色，取消显示下划线
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas); // 调用父类的onDraw方法，确保绘制流程正常
        int lineCount = getLineCount(); // 获取文本的总行数
        Rect r = mRect; // 获取一个矩形对象，用于存储每行的边界信息
        Paint p = mPaint; // 获取画笔对象，用于绘制线条
        // 遍历所有行
        for (int i = 0; i < lineCount; i++) {
            int baseline = getLineBounds(i, r); // 获取当前行的基线位置和边界，存储在矩形对象r中
            // 在每行基线下方20像素处绘制一条横跨整个行宽的横线
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, p);
        }
    }
}